package com.ty.controller;

import com.ty.entity.Order;
import com.ty.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单信息对象功能接口")
@RestController
@RequestMapping("/api/order")
public class OrderController extends BaseController{

    @Autowired
    OrderService orderService;

    @ApiOperation("订单生成")
    @PostMapping("/insert")
    public ResponseEntity<Boolean> insert (Order order){
        boolean rs =orderService.save(order);
        return ResponseEntity.ok(rs);
    }


    @ApiOperation("通过ID查询单条数据")
    @GetMapping("/queryById")
    public ResponseEntity<Order> queryById(Integer orderId){
        Order rs =  orderService.queryById(orderId);
        return ResponseEntity.ok(rs);
    }


    @ApiOperation("更新数据")
    @PostMapping("/update")
    public ResponseEntity<Integer> update(Order order){
        int rs = orderService.update(order);
        return ResponseEntity.ok(rs);
    }

    @ApiOperation("删除订单")
    @DeleteMapping("/delete")
    public ResponseEntity<Integer> delete(Integer orderId){
        int rs =orderService.deleteById(orderId);
        return ResponseEntity.ok(rs);
    }

    @ApiOperation("将订单修改为支付状态")
    @PostMapping("/changestatus")
    public ResponseEntity<Integer> updateStatusToPaySuccess(Integer orderId){
        int rs =orderService.updateStatusToPaySuccess(orderId);
        return ResponseEntity.ok(rs);
    }

    @ApiOperation("取消订单")
    @PostMapping("/remove")
    public ResponseEntity<Integer> cancelOrders(Integer orderId){
        int rs = orderService.cancelOrders(orderId);
        return ResponseEntity.ok(rs);
    }

    @ApiOperation("根据订单号和用户id获取订单")
    @GetMapping("/getorder")
    public ResponseEntity<String> getByOrderIdAndUserId(Integer orderId,Integer userId){
        List list =orderService.getByOrderIdAndUserId(orderId,userId);
        return ResponseEntity.ok(list.toString());
    }

    @ApiOperation("确认收货")
    @PostMapping("/require")
    public ResponseEntity<Integer> receiedOrder(Integer orderId){
        int rs =orderService.receiedOrder(orderId);
        return ResponseEntity.ok(rs);
    }
}
